﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Portal.Models;

namespace Portal.Filter
{
    /// <summary>
    /// 检查用户是否可以访问的过滤器
    /// </summary>
    public class LoginCheckFilterAttribute:ActionFilterAttribute
    {
        /// <summary>
        /// 是否校验
        /// </summary>
        public bool IsCheck { get; set; }


        /// <summary>
        /// 在进入Action方法之前执行的过滤器
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);

            if (IsCheck == false)
            {
                return;
            }

            if (context.HttpContext.Session.GetString(GlobalVariable.LoginUser) == null)
            {
                //重定向到登陆页面
                context.HttpContext.Response.Redirect("/Login/Index");
            }
        }
    }
}